/**
 * Created by hubo on 2016/4/28.
 */
$(function() {
    getExtParam();
    addIpList();
});


//添加ip控件
function addIpList(){
    var MaxLength = 8;
    var divParam = $("#divParam");
    var addParam = $("#addParam");
    var len =  divParam.length;
    var tag = 1;

    $("#addParam").click(function(e){
        if(len<=MaxLength){
            tag++;
            $("#paramList").append("<div><input type='text'  class='form-control'  value=''  name='extParam' id='Param_"+ tag +"'  placeholder='key:value'/><a style='color: #1AB394;' href='#' class='removeclass pull-right fa fa-times'></a></div>");
            len++;
        }
        return false;
    });
    $("body").on("click",".removeclass",function(e){
        console.log(len);
        if(len>=1){
            $(this).parent("div").remove();
            //len--;
        }
        return false;
    });

}




//查询扩展参数列表
function  getExtParam(){
    var partner = getQueryString("partner");
    $.ajax({
        url:ctx+"/developer/getExtParam",
        type:'post',
        data:{
            "partner":partner
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("查询扩展参数失败")
        },
        success:function(json){
            var paramList = json.result;
            delete paramList.partner;
            for(var key in paramList){
                $("#paramList").append("<div><input name='extParam' type='text' id='"+key+"' value='"+key+":"+paramList[key]+"'  class='form-control' /><a href='#' style='color: #1AB394;' onclick='delExtParam(\""+key+"\")'  class='removeclass pull-right fa fa-times' ></a><div>");
            }
        }
    });
}

//删除扩展参数
function delExtParam(key){

    var partner = getQueryString("partner");
    $.ajax({
        url:ctx+"/developer/delExtParam",
        type:'post',
        data:{
            "partner":partner,
            "extName":key
        },
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("删除扩展参数失败")
        },
        success:function(json){
            if(json.returnCode=='T'){
                toastr.success("删除成功");
                setTimeout('skipParamList()',1000);
            }else{
                toastr.error("删除扩展参数失败")
            }

        }
    });

}

function skipParamList(){
    var partner = getQueryString("partner");
    location.href=ctx+"/send/developer/paramList?partner="+partner;
}
//获取url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

//添加获取开发者id
$("#addParameter").click(function(){
    var partner = getQueryString("partner");
    $("#HiddenPartner").val(partner);
});


//保存扩展参数
$("#addExtParam").click(function(){
    var partner = getQueryString("partner");
    var extParam = $(":input[name='extParam']").map(function(){
        return $(this).val();
    }).get();
    var data={};
    if(extParam==""){
        toastr.error("扩展参数不能为空");
        return;
    }
    data.partner=partner;
    var reg = /^(?=[0-9a-zA-Z:]+$)/;
    for(var i=0;i<extParam.length;i++){
        var array = extParam[i].split(":");
        if (array[1] == undefined) {
            var key = array[0];
            var value = array[1];
        } else {
            var key = array[0].trim();
            var value = array[1].trim();
        }
        data[key] = value;
        if(value==undefined||!reg.test(key)){
            toastr.error("扩展参数格式不正确请重新输入(key:value)");
            return;
        }
    }

    $.ajax({
        url:ctx+"/developer/updateExtParam",
        type:'post',
        data:data,
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){

            toastr.error("操作失败")
        },
        success:function(json){
            if(json.returnCode=='T'){
                toastr.success("操作成功");
                setTimeout('skipParamList()',1000);
            }else{
                toastr.error("操作失败")
            }
        }
    });

});


